<template>
  <button type="button" v-on:click="run" :disabled="disabled || time > 0">{{ text }}</button>
</template>

<script>
  export default {
    data: function () {
      return {
        time: 0,
        disabled: false
      }
    },
    methods: {
      run: function () {
        this.$emit('run');
      },
      start: function () {
        this.time = 60;
        this.timer();
      },
      stop: function () {
        this.time = 0;
        this.disabled = false;
      },
      setDisabled: function (val) {
        this.disabled = val;
      },
      timer: function () {
        if (this.time > 0) {
          this.time--;
          setTimeout(this.timer, 1000);
        } else {
          this.disabled = false;
        }
      }
    },
    computed: {
      text: function () {
        return this.time > 0 ? this.time + '秒重新发送' : '获取验证码';
      }
    }
  }
</script>
